home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / second / second.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-22  |  3.2 KB  |  163 lines

  1. /* Quick test program to get exec'd. */
  2.  
  3. #include <sprite.h>
  4. #include <cfuncproto.h>
  5. #include <errno.h>
  6. #include <mach.h>
  7. #include <status.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <test.h>
  12. #include <vm.h>
  13.  
  14. static void CatFile();
  15. static int GetLength _ARGS_((char *fileName));
  16. static void MapFile _ARGS_((char *fileName, boolean_t readOnly,
  17.                 int length, Address *startAddrPtr));
  18.  
  19. int
  20. main(argc, argv)
  21.     int argc;
  22.     char *argv[];
  23. {
  24.     int i;
  25.  
  26.     /* print the arguments */
  27.     Test_PutDecimal(argc);
  28.     Test_PutMessage(" arguments: \n");
  29.     for (i = 0; i < argc; i++) {
  30.     Test_PutMessage(argv[i]);
  31.     Test_PutMessage("\n");
  32.     }
  33.  
  34.     /* if there was a file named, cat it */
  35.     for (i = 1; i < argc; i++) {
  36.     if (argv[i][0] == '-') {
  37.         continue;
  38.     }
  39.     CatFile(argv[i]);
  40.     }
  41.  
  42.     return 0;
  43. }
  44.  
  45.  
  46. /*
  47.  *----------------------------------------------------------------------
  48.  *
  49.  * CatFile --
  50.  *
  51.  *    Print the contents of a file.
  52.  *
  53.  * Results:
  54.  *    None.
  55.  *
  56.  * Side effects:
  57.  *    None.
  58.  *
  59.  *----------------------------------------------------------------------
  60.  */
  61.  
  62. static void
  63. CatFile(fileName)
  64.     char *fileName;
  65. {
  66.     int fileLength;
  67.     char *buffer;
  68.  
  69.     Test_PutMessage("----- ");
  70.     Test_PutMessage(fileName);
  71.     Test_PutMessage(" -----\n");
  72.  
  73.     if (strcmp(fileName, "killMe") == 0) {
  74.     *(char *)0xffffffff = fileName[0];
  75.     }
  76.     if (strcmp(fileName, "nullArg") == 0) {
  77.     fileName = (char *)0;
  78.     }
  79.     fileLength = GetLength(fileName);
  80.     if (fileLength > 0) {
  81.     MapFile(fileName, TRUE, fileLength, &buffer);
  82.     if (buffer != 0) {
  83.         Test_PutString(buffer, fileLength);
  84.         vm_deallocate(mach_task_self(), (vm_address_t)buffer, fileLength);
  85.     }
  86.     }
  87. }
  88.  
  89.  
  90. /*
  91.  *----------------------------------------------------------------------
  92.  *
  93.  * GetLength --
  94.  *
  95.  *    Get the length of a file.
  96.  *
  97.  * Results:
  98.  *    Returns the length of the file, in bytes.  Returns -1 if there 
  99.  *    was an error.
  100.  *
  101.  * Side effects:
  102.  *    None.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. static int
  108. GetLength(fileName)
  109.     char *fileName;
  110. {
  111.     struct stat statBuf;
  112.  
  113.     if (stat(fileName, &statBuf) < 0) {
  114.     Test_PutMessage("Couldn't get length of `");
  115.     Test_PutMessage(fileName);
  116.     Test_PutMessage("': ");
  117.     Test_PutMessage(strerror(errno));
  118.     Test_PutMessage("\n");
  119.     return -1;
  120.     }
  121.  
  122.     return statBuf.st_size;
  123. }
  124.  
  125.  
  126. /*
  127.  *----------------------------------------------------------------------
  128.  *
  129.  * MapFile --
  130.  *
  131.  *    Map the named file into our address space.
  132.  *
  133.  * Results:
  134.  *    Fills in the starting location, which is set to 0 
  135.  *    if there was a problem.
  136.  *
  137.  * Side effects:
  138.  *    None.
  139.  *
  140.  *----------------------------------------------------------------------
  141.  */
  142.  
  143. static void
  144. MapFile(fileName, readOnly, length, startAddrPtr)
  145.     char *fileName;        /* name of file to map */
  146.     Boolean readOnly;        /* map read-only or read-write? */
  147.     int length;            /* number of bytes to map */
  148.     Address *startAddrPtr;    /* OUT: where the file was mapped to */
  149. {
  150.     ReturnStatus status;
  151.  
  152.     status = Vm_MapFile(fileName, readOnly, 0, length, startAddrPtr);
  153.     if (status != SUCCESS) {
  154.     Test_PutMessage("Couldn't map `");
  155.     Test_PutMessage(fileName);
  156.     Test_PutMessage("': ");
  157.     Test_PutMessage(Stat_GetMsg(status));
  158.     Test_PutMessage("\n");
  159.     *startAddrPtr = 0;
  160.     }
  161. }
  162.  
  163.